<template>
	<view class="mainpadding2">
		<u-navbar title="優惠卡類型" placeholder autoBack>
			<view class="flexright xiaohui" slot="right" @click="show=true">添加</view>
		</u-navbar>
		<view class="flexcenter" v-if="!list.length">
			<view class="placeholderimage" style="margin-top: 100rpx;"></view>
		</view>
		<view class="mainpadding_top ffffff radius" v-for="item in list" :key="item.id" @click="checkitem(item)">
			<view class="titletext xiaohuang bold">{{item.name}}</view>
			<view class="flexright margin_top">
				<view class="flexleft margin_left" @click.stop="edititem(item)">
					<u-icon name="edit-pen" color="#999" size="16"></u-icon>
					<view class="xiaohei">編輯</view>
				</view>
				<view class="flexleft margin_left" @click.stop="delitem(item.id)">
					<u-icon name="trash" color="#999" size="16"></u-icon>
					<view class="xiaohei">刪除</view>
				</view>
			</view>
		</view>
		<u-modal :show="show" title="優惠卡類型" confirmText="確認" confirmColor="#FF5C00" @confirm="chongzhi" cancelText="取消"
			@cancel="show=false" showCancelButton>
			<view class="slot-content" style="width: 600rpx;">
				<view class="flexbetween margin_top">
					<view class="main_size bold hei_text">名稱：</view>
					<input type="text" class="textright" placeholder="請输入優惠卡類型名稱" v-model="name">
				</view>
				<view class="margin_top">
					<view class="main_size bold hei_text" style="min-width: 120rpx;">住宿方式：</view>
					<view class="flexright margin_top2">
						<u-checkbox-group v-model="checkboxValue1" placement="row">
							<u-checkbox activeColor="#feb800" :customStyle="{marginRight: '10rpx'}"
								v-for="(item, index) in checkboxList2" :key="index" :label="item.name"
								:name="item.value">
							</u-checkbox>
						</u-checkbox-group>
					</view>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				show: false,
				weigh: "",
				name: "",
				editid: "",
				type: 0,
				checkboxList2: [],
				checkboxValue1: [],
				zsfs: "",
			}
		},
		onLoad(options) {
			if (options.type == 1) {
				this.type = 1
				this.zsfs = options.zsfs
			}
			this.getlxList()
			this.getInfo()
		},
		methods: {
			getInfo() {
				this.http.request("/api/shop/Shopdeail", "POST", {
					shop_id: uni.getStorageSync("shop_id"),
				}).then(res => {
					if (res.code == 1) {
						let zslxlist = getApp().globalData.zslxlist
						let arr = res.data.zs_type ? res.data.zs_type.split(',') : []
						let checkboxList2 = []
						arr.forEach(item => {
							zslxlist.forEach(ite=>{
								if(item==ite.value){
									checkboxList2.push(ite)
								}
							})
						})
						this.checkboxList2 = checkboxList2
					}
				})
			},
			checkitem(item) {
				if (this.type == 1) {
					uni.$emit("checkyhklx", item)
					uni.navigateBack(1)
				}
			},
			getlxList() {
				this.http.request("/api/Hotel/hotelcardTypeIndex", 'GET', {
					shop_id: uni.getStorageSync("shop_id"),
					zs_type: this.zsfs
				}).then(res => {
					this.list = res.data
				})
			},
			chongzhi() {
				if (this.name == "") {
					this.http.toast("請輸入優惠卡類型名稱")
					return false
				}
				let url = "/api/Hotel/addHotelcardType"
				let data = {
					shop_id: uni.getStorageSync("shop_id"),
					name: this.name,
					zs_type: this.checkboxValue1.join()
				}
				if (this.editid) {
					url = "/api/Hotel/editHotelcardType"
					this.$set(data, "id", this.editid)
				}
				this.http.request(url, 'POST', data).then(res => {
					this.http.toast(res.msg)
					if (res.code == 1) {
						this.show = false
						this.name = ""
						this.editid = ""
						this.checkboxValue1 = []
						this.getlxList()
					}
				})
			},
			edititem(item) {
				this.editid = item.id
				this.name = item.name
				this.checkboxValue1 = item.zs_type.split(',')
				this.show = true
			},
			delitem(id) {
				this.http.modal('提示', '確認刪除當前數據嗎？', true, (res) => {
					if (res) {
						this.http.request("/api/hotel/delHotelcardType", 'GET', {
							id
						}).then(res => {
							if (res.code == 1) {
								this.http.toast("刪除成功")
								this.getlxList()
							} else {
								this.http.toast(res.msg);
							}
						})

					}
				})
			},
		}
	}
</script>

<style>

</style>